﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using log4net;

namespace ArrowWebParts
{
    class DashboardControllerEditorPart : EditorPart
    {
        #region fields

        //private string dataSource;
        //private string selectStatement;

        private TextBox dataSourceTextBox;
        private TextBox selectStatementTextBox;

        private static ILog log = LogManager.GetLogger(typeof(DashboardControllerEditorPart));

        #endregion

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (log.IsDebugEnabled)
                log.Debug("Creating child controls.");

            try
            {
                Title = "Arrow Dashboard Controller Properties";

                ToolPane pane = Zone as ToolPane;
                if (pane != null)
                    pane.Cancel.CausesValidation = false;

                generateAndAddEditorPartTextBox("SqlServer Connection String", "Input the connection string to the target SqlServer database.", out dataSourceTextBox);
                //dataSourceTextBox.ID = "dataSourceTextBox";

                Controls.Add(new LiteralControl("<br/><DIV class=\"UserDottedLine\" width=\"100%\"></DIV>"));
                generateAndAddEditorPartTextBox("SqlServer Select Statement", "Input the select statement of the target SqlServer database.", out selectStatementTextBox);
            }
            catch (Exception ex)
            {
                ArrowUtils.LogFatalException(log, ex);
                Controls.Clear();
                Controls.Add(new LiteralControl(string.Format("There was an error creating the dashboard controller editor part: {0}", ex.Message)));
                return;
            }

            if (log.IsDebugEnabled)
                log.Debug("Finished creating child controls.");
        }

        public override void SyncChanges()
        {
            if (log.IsDebugEnabled)
                log.Debug("Syncing changes.");

            try
            {
                this.EnsureChildControls();
                DashboardControllerWebPart sourcePart = (DashboardControllerWebPart)this.WebPartToEdit;
                //this.dataSource = sourcePart.DataSource;
                //this.selectStatement = sourcePart.SelectStatement;
                dataSourceTextBox.Text = sourcePart.DataSource;
                selectStatementTextBox.Text = sourcePart.SelectStatement;
            }
            catch (Exception ex)
            {
                ArrowUtils.LogFatalException(log, ex);
                Controls.Clear();
                Controls.Add(new LiteralControl(string.Format("There was an error syncing changes: {0}", ex.Message)));
                return;
            }

            if (log.IsDebugEnabled)
                log.Debug("Finished syncing changes.");
        }

        public override bool ApplyChanges()
        {
            if (log.IsDebugEnabled)
                log.Debug("Applying changes.");

            try
            {
                this.EnsureChildControls();
                DashboardControllerWebPart targetPart = (DashboardControllerWebPart)this.WebPartToEdit;
                if (targetPart != null)
                {
                    //targetPart.DataSource = dataSource;
                    //targetPart.SelectStatement = selectStatement;
                    targetPart.DataSource = dataSourceTextBox.Text.Trim();
                    targetPart.SelectStatement = selectStatementTextBox.Text.Trim();
                }
                else
                {
                    if (log.IsDebugEnabled)
                        log.Debug("Finished applying changes.");
                    return false;
                }

                if (log.IsDebugEnabled)
                    log.Debug("Finished applying changes.");

                return true;
            }
            catch (Exception ex)
            {
                ArrowUtils.LogFatalException(log, ex);
                Controls.Clear();
                Controls.Add(new LiteralControl(string.Format("There was an error applying changes: {0}", ex.Message)));
                return false;
            }
        }

        private void generateAndAddEditorPartTextBox(string propertyName, string toolTip, out TextBox tex)
        {
            Controls.Add(new LiteralControl("<DIV class=\"UserSectionHead\">"));
            Controls.Add(new Label { Text = propertyName, ToolTip = toolTip });
            Controls.Add(new LiteralControl("</DIV>"));
            Controls.Add(new LiteralControl("<DIV class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<DIV class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<NOBR>"));
            tex = new TextBox();
            tex.CssClass = "UserInput";
            Controls.Add(tex);
            Controls.Add(new LiteralControl("<NOBR>"));
            Controls.Add(new LiteralControl("<DIV class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<DIV class=\"UserControlGroup\">"));
        }
    }
}
